﻿using System;
using System.Collections.Generic;
using System.Web;
using SYS.Web.Runtime;
using SYS.Common.Runtime;

namespace ExpertMIS.Codes
{
    public static class Utility
    {
        public static string GetClientIP()
        {
            if (HttpContext.Current != null)
            {
                return HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }
            else
            {
                return "unknown";
            }
        }

        public static HttpEnvironment GetEnvironment()
        {
            return (HttpEnvironment)EnvironmentFactory.CreateEnvironment(EnvironmentType.ASPDotNet);
        }

        public static string GetWebRoot(System.Web.UI.Control control, string relativeUrl)
        {
            return control.ResolveUrl(relativeUrl);
        }

        public static string CutLongText(string text)
        {
            SYS.Common.Broker.EnvironmentBroker broker = new SYS.Common.Broker.EnvironmentBroker();
            SYS.Common.Broker.EnvironmentEntity variable = broker.GetEnvironmentValue("cutlongtext", false) as SYS.Common.Broker.EnvironmentEntity;

            int limit = 20;
            if (variable != null)
            {
                int.TryParse(variable.Value, out limit);
            }

            if (limit > 0 && text.Length >= limit)
            {
                return text.Substring(0, limit) + "...";
            }
            return text;
        }

        public static string CutLongText(string text, int limit)
        {
            if (limit > 0 && text.Length >= limit)
            {
                return text.Substring(0, limit) + "...";
            }
            return text;
        }

        public static string CutLongText(object text, int limit)
        {
            if (text != null && string.IsNullOrEmpty(text.ToString())) return "";
            if (limit > 0 && text.ToString().Length >= limit)
            {
                return text.ToString().Substring(0, limit) + "...";
            }
            return text.ToString();
        }

        public static string GetEnvironmentValue(string variableName)
        {
            SYS.Common.Broker.EnvironmentBroker broker = new SYS.Common.Broker.EnvironmentBroker();
            SYS.Common.Broker.EnvironmentEntity variable = broker.GetEnvironmentValue(variableName, false) as SYS.Common.Broker.EnvironmentEntity;

            if (variable != null)
            {
                return variable.Value;
            }
            else
            {
                return string.Empty;
            }
        }

        public static string GetCurrentUsername()
        {
            if (null != GetEnvironment().GetCurrentUser())
            {
                return GetEnvironment().GetCurrentUser().Name;
            }
            else
            {
                return "游客";
            }
        }

        public static bool IsLogon
        {
            get
            {
                return GetEnvironment().Logon;
            }
        }

        public static string ReplaceNLC2BR(string originalStr)
        {
            return originalStr.Replace("\r\n", "<br/>");
        }
    }
}
